<?php
namespace app\admin\controller;

class Product extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'Product';
    }
    //
    protected function _before_index(){
        $thead = [
            'pid' => 'PID',
            'pname' => '商品名称',
            'cid' => '商品分类',
            'price' => '销售价格',
            'oprice' => '商品原价',
            'sale' => '商品状态',
            'desc' => '备注信息',
            'addtime' =>'创建时间',
        ];
        $this->assign('thead', $thead);
        $cateArr = model('Category')->where('status', '=', 1)->select()->toArray();
        $this->assign('cateArr', $cateArr);
    }
    //
    protected function _filter($map){
        return $map;
    }
    //
    protected function _after_list($list){
        $cateArr = model('Category')->column('cname', 'cid');
        foreach($list as $key => $row){
            $list[$key]['cid'] = $cateArr[$row['cid']] ?? '';
            $list[$key]['sale'] = $row['sale'] == 1 ? '上架' : '下架';
            $list[$key]['addtime'] = date('Y-m-d H:i:s', $row['addtime']);
        }
        return $list;
    }
    //
    protected function _before_add(){
        $cateArr = model('Category')->where('status', '=', 1)->select()->toArray();
        $this->assign('cateArr', $cateArr);
    }
    //
    protected function _before_insert($data){
        $data['addtime'] = time();
        return $data;
    }
    //
    protected function _after_add($data, $id){
        $this->log(['pk' => 'pid', 'value' => $id], $data);
    }
    //
    protected function _before_edit(){
        $this->_before_add();
    }
    //
    protected function _filter_edit($list){
        return $list;
    }
    //
    protected function _before_save($data){
        return $data;
    }
    //
    protected function _after_edit($data){
        $this->log(['pk' => 'pid', 'value' => $data['pid']], $data);
    }
    //
    protected function _filter_del(){
        return [1];
    }
    //
    protected function _after_del($rows){
        $logs = [];
        foreach($rows as $row){
            $logs[] = ['pk' => 'pid', 'value' => $row['pid'], 'row' => $row];
        }
        $this->logAll($logs);
    }
}